#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int main() {
    // 创建客户端 Socket
    int client_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (client_fd == -1) {
        std::cerr << "Error: Could not create socket\n";
        return 1;
    }

    // 连接到服务器
    sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 服务器 IP 地址
    server_addr.sin_port = htons(8080); // 服务器端口号

    if (connect(client_fd, (sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
        std::cerr << "Error: Connection failed\n";
        return 1;
    }

    std::cout << "Connected to server\n";

    // 接收服务器消息
    char buffer[1024] = {0};
    int valread = read(client_fd, buffer, 1024);
    std::cout << "Server message: " << buffer << "\n";

    // 关闭连接
    close(client_fd);

    return 0;
}
